home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / FREENET / MELL / NETLIB00 / NetLib / c / ntop < prev    next >
Text File  |  1996-06-27  |  4KB  |  164 lines

  1. /* Copyright (c) 1996 by Internet Software Consortium.
  2.  *
  3.  * Permission to use, copy, modify, and distribute this software for any
  4.  * purpose with or without fee is hereby granted, provided that the above
  5.  * copyright notice and this permission notice appear in all copies.
  6.  *
  7.  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  8.  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  9.  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  10.  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  12.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  13.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  14.  * SOFTWARE.
  15.  */
  16.  
  17. #include "netinet/in.h"
  18. #include "arpa/inet.h"
  19. #include "Internet:sys.h.byteorder"
  20. #include <ctype.h>
  21. #include "arpa/nameser.h"
  22. #include <string.h>
  23. #include "sys/errno.h"
  24. #include "sys/socket.h"
  25. #include <stdio.h>
  26.  
  27. /* const char *
  28.  * inet_ntop4(src, dst, size)
  29.  *    format an IPv4 address, more or less like inet_ntoa()
  30.  * return:
  31.  *    `dst' (as a const)
  32.  * notes:
  33.  *    (1) uses no statics
  34.  *    (2) takes a u_char* not an in_addr as input
  35.  * author:
  36.  *    Paul Vixie, 1996.
  37.  */
  38. static const char *inet_ntop4(const u_char *src, char *dst, size_t size)
  39. {
  40.     static const char fmt[] = "%u.%u.%u.%u";
  41.     char tmp[sizeof "255.255.255.255"];
  42.  
  43.     if (sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) > size) {
  44.         errno = ENOSPC;
  45.         return NULL;
  46.     }
  47.     return strcpy(dst, tmp);
  48. }
  49.  
  50. /* const char *
  51.  * inet_ntop6(src, dst, size)
  52.  *    convert IPv6 binary address into presentation (printable) format
  53.  * author:
  54.  *    Paul Vixie, 1996.
  55.  */
  56. static const char *inet_ntop6(const u_char *src, char *dst, size_t size)
  57. {
  58.     /*
  59.      * Note that int32_t and int16_t need only be "at least" large enough
  60.      * to contain a value of the specified size.  On some systems, like
  61.      * Crays, there is no such thing as an integer variable with 16 bits.
  62.      * Keep this in mind if you think this function should have been coded
  63.      * to use pointer overlays.  All the world's not a VAX.
  64.      */
  65.     char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
  66.     struct { int base, len; } best, cur;
  67.     u_int words[IN6ADDRSZ / INT16SZ];
  68.     int i;
  69.  
  70.     /*
  71.      * Preprocess:
  72.      *    Copy the input (bytewise) array into a wordwise array.
  73.      *    Find the longest run of 0x00's in src[] for :: shorthanding.
  74.      */
  75.     memset(words, 0, sizeof words);
  76.     for (i = 0; i < IN6ADDRSZ; i++)
  77.         words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
  78.     best.base = -1;
  79.     cur.base = -1;
  80.     for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
  81.         if (words[i] == 0) {
  82.             if (cur.base == -1)
  83.                 cur.base = i, cur.len = 1;
  84.             else
  85.                 cur.len++;
  86.         } else {
  87.             if (cur.base != -1) {
  88.                 if (best.base == -1 || cur.len > best.len)
  89.                     best = cur;
  90.                 cur.base = -1;
  91.             }
  92.         }
  93.     }
  94.     if (cur.base != -1) {
  95.         if (best.base == -1 || cur.len > best.len)
  96.             best = cur;
  97.     }
  98.     if (best.base != -1 && best.len < 2)
  99.         best.base = -1;
  100.  
  101.     /*
  102.      * Format the result.
  103.      */
  104.     tp = tmp;
  105.     for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
  106.         /* Are we inside the best run of 0x00's? */
  107.         if (best.base != -1 && i >= best.base &&
  108.             i < (best.base + best.len)) {
  109.             if (i == best.base)
  110.                 *tp++ = ':';
  111.             continue;
  112.         }
  113.         /* Are we following an initial run of 0x00s or any real hex? */
  114.         if (i != 0)
  115.             *tp++ = ':';
  116.         /* Is this address an encapsulated IPv4? */
  117.         if (i == 6 && best.base == 0 &&
  118.             (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
  119.             if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
  120.                 return (NULL);
  121.             tp += strlen(tp);
  122.             break;
  123.         }
  124.         tp += sprintf(tp, "%x", words[i]);
  125.     }
  126.     /* Was it a trailing run of 0x00's? */
  127.     if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
  128.         *tp++ = ':';
  129.     *tp++ = '\0';
  130.  
  131.     /*
  132.      * Check for overflow, copy, and we're done.
  133.      */
  134.     if ((tp - tmp) > size) {
  135.         errno = ENOSPC;
  136.         return NULL;
  137.     }
  138.     return strcpy(dst, tmp);
  139. }
  140.  
  141.  
  142. /* char *
  143.  * inet_ntop(af, src, dst, size)
  144.  *    convert a network format address to presentation format.
  145.  * return:
  146.  *    pointer to presentation format address (`dst'), or NULL (see errno).
  147.  * author:
  148.  *    Paul Vixie, 1996.
  149.  */
  150. const char *inet_ntop (int af, const void *src, char *dst, size_t size)
  151. {
  152.     switch (af) {
  153.     case AF_INET:
  154.         return (inet_ntop4(src, dst, size));
  155.     case AF_INET6:
  156.         return (inet_ntop6(src, dst, size));
  157.     default:
  158.         errno = EAFNOSUPPORT;
  159.         return (NULL);
  160.     }
  161.     /* NOTREACHED */
  162. }
  163.  
  164.